﻿using BPMN.Application.Contracts.DTO.Request.Routing;
using BPMN.Application.Contracts.DTO.Response;
using BPMN.Application.IService;
using BPMN.Domain.Entity.Routing;
using BPMN.Domain.Entity.Unit;
using BPMN.Domain.Entity.User;
using BPMN.Domain.Interface;
using BPMN.Domain.ProcessModel;
using BPMN.DomianShare;
using Microsoft.AspNetCore.Mvc;
using static Dapper.SqlMapper;

namespace BPMN.Application.Service
{
    public class RoutingService : AppServiceBase<Routings>, IRoutingService
    {
        private readonly IRepositoryBase<Routings> routing;
        private readonly IRepositoryBase<RoutingProcess> routingProcess;
        private readonly IRepositoryBase<ProcessShows> process;

        public RoutingService(IRepositoryBase<Routings> routing, IRepositoryBase<RoutingProcess> routingProcess, IRepositoryBase<ProcessShows> process) : base(routing)
        {
            this.routing = routing;
            this.routingProcess = routingProcess;
            this.process = process;
        }

        /// <summary>
        /// 添加工艺路线-工序列表信息
        /// </summary>
        /// <param name="dto">工艺路线工序关联信息</param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> AddProcess(ProcessDto dto)
        {
            RoutingProcess info = new RoutingProcess();
            info.TypeId = dto.TypeId;
            info.ProcessName = dto.Process_Name;
            info.RoutingName = dto.RoutingName;
            info.CreatorId = dto.CreateId;
            info.CreateDate = DateTime.Now;

            int n = await routingProcess.Add(info);
            if (n > 0)
            {
                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "添加失败");
            }
        }

        /// <summary>
        /// 添加工艺路线信息
        /// </summary>
        /// <param name="dto">工艺路线信息</param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> AddRouting(Routings dto)
        {
            var slist = routing.GetAll().Result.Where(x => x.Name == dto.Name && x.IsDel == true).FirstOrDefault();
            if (slist != null)
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "工艺路线名称已存在");
            }

            dto.CreateDate = DateTime.Now;
            dto.UpdateDate = DateTime.Now;
            int n = await routing.Add(dto);
            if (n > 0)
            {
                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "添加失败");
            }
        }

        /// <summary>
        /// 删除工艺路线信息
        /// </summary>
        /// <param name="id">工艺路线Id</param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> DeleleRouting(int id)
        {
            var obj = await routing.Get(id);
            obj.IsDel = true;
            int n = await routing.Update(obj);
            if (n > 0)
            {
                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "逻辑删除失败");
            }
        }

        /// <summary>
        /// 获取工艺路线-工序列表信息
        /// </summary>
        /// <param name="id">工艺路线Id</param>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<ProcessDto>>> GetProcess(int id)
        {
            var obj = routing.GetAll().Result.Where(a => a.Id == id);
            List<ProcessDto> dtos = new List<ProcessDto>();
            ProcessDto dto = new ProcessDto();
            foreach (var item in obj.ToList())
            {
                dto.Name = item.Name;
                dto.RoutingName = item.Name;

                var list = routingProcess.GetAll().Result.Where(a => a.ProcessName == item.Name);
                foreach (var item1 in list.ToList())
                {
                    dto.TypeId = item1.TypeId;
                    dto.Process_Name = item1.ProcessName;
                    dto.CreateId = item1.Id;
                    dto.UpdateId = item1.Id;
                    dto.CreateTime = DateTime.Now;
                    dto.UpdateTime = DateTime.Now;
                    var list1 = process.GetAll().Result.Where(a => a.Process_Name == item1.ProcessName);
                    foreach (var item2 in list1.ToList())
                    {
                        dto.Report = item2.Process_DuiBi;

                        dtos.Add(dto);
                    }
                }
            }

            return new ResultResponseDto<List<ProcessDto>>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = dtos
            };
        }

        /// <summary>
        /// 分页、显示、查询工艺路线信息
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页显示</param>
        /// <param name="code">工艺路线编码</param>
        /// <param name="name">工艺路线名称</param>
        /// <returns></returns>
        public async Task<ResultResponseDto<RoutingDto>> GetRouting(int pageIndex, int pageSize, string code, string name)
        {
            var obj = routing.GetAll().Result.Where(a => a.IsDel == false);
            if (!string.IsNullOrEmpty(code))
            {
                obj = obj.Where(x => x.Code.Contains(code));
            }
            if (!string.IsNullOrEmpty(name))
            {
                obj = obj.Where(x => x.Name.Contains(name));
            }

            int total = obj.Count();
            int page = (int)Math.Ceiling((double)total / pageSize);
            obj = obj.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            RoutingDto dto = new RoutingDto();
            dto.TotalCount = total;
            dto.PageCount = page;
            dto.List = obj.ToList();

            return new ResultResponseDto<RoutingDto>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = dto
            };
        }

        /// <summary>
        /// 显示工艺路线信息
        /// </summary>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<Routings>>> GetRoutings()
        {
            var obj = routing.GetAll().Result.Where(a => a.IsDel == false);
            return new ResultResponseDto<List<Routings>>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = obj.ToList()
            };
        }

        /// <summary>
        /// 反填工艺路线信息
        /// </summary>
        /// <param name="id">工艺路线Id</param>
        /// <returns></returns>
        public async Task<ResultResponseDto<Routings>> GetRoutingById(int id)
        {
            var slist = await routing.Get(id);
            return new ResultResponseDto<Routings>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = slist
            };
        }

        /// <summary>
        /// 修改工艺路线信息
        /// </summary>
        /// <param name="dto">工艺路线</param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> UpdateRouting(Routings dto)
        {
            dto.UpdateDate = DateTime.Now;
            int n = await routing.Update(dto);
            if (n > 0)
            {
                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "修改失败");
            }
        }
    }
}
